home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / WINER.ZIP / CHAP6-3.BAS < prev    next >
BASIC Source File  |  1992-05-13  |  2KB  |  80 lines

  1. '*********** CHAP6-3.BAS - random access file demonstration
  2.  
  3. 'Copyright (c) 1992 Ethan Winer
  4.  
  5. '----- create a data file containing five records
  6. DEFINT A-Z
  7.  
  8. TYPE MyType
  9.   FirstName AS STRING * 17
  10.   LastName  AS STRING * 14
  11.   DblValue  AS DOUBLE
  12.   IntValue  AS INTEGER
  13.   MiscStuff AS STRING * 20
  14.   SngValue  AS SINGLE
  15. END TYPE
  16. DIM MyVar AS MyType
  17.  
  18. OPEN "MYFILE.DAT" FOR RANDOM AS #1 LEN = 65
  19. MyVar.FirstName = "Jonathan"
  20. MyVar.LastName = "Smith"
  21. MyVar.DblValue = 123456.7
  22. MyVar.IntValue = 10
  23. MyVar.MiscStuff = "Miscellaneous stuff"
  24. MyVar.SngValue = 14.29
  25. FOR X = 1 TO 5
  26.   PUT #1, , MyVar
  27.   MyVar.DblValue = MyVar.DblValue * 2
  28.   MyVar.IntValue = MyVar.IntValue * 2
  29.   MyVar.SngValue = MyVar.SngValue * 2
  30. NEXT
  31. CLOSE #1
  32.  
  33.  
  34. '----- read the data without regard to the TYPE above
  35. READ FileName$, NumFields
  36. REDIM Buffer$(1 TO NumFields)   'holds the FIELD strings
  37. REDIM FieldType(1 TO NumFields) 'the array of data types
  38.  
  39. RecLength = 0
  40. FOR X = 1 TO NumFields
  41.   READ ThisType
  42.   FieldType(X) = ThisType
  43.   RecLength = RecLength + ABS(ThisType)
  44. NEXT
  45.  
  46. OPEN FileName$ FOR RANDOM AS #1 LEN = RecLength
  47.  
  48. PadLength = 0
  49. FOR X = 1 TO NumFields
  50.   ThisLength = ABS(FieldType(X))
  51.   FIELD #1, PadLength AS Pad$, ThisLength AS Buffer$(X)
  52.   PadLength = PadLength + ThisLength
  53. NEXT
  54. CLS
  55. NumRecs = LOF(1) \ RecLength    'calc number of records
  56. FOR X = 1 TO NumRecs            'read each in sequence
  57.   GET #1                        'get the current record
  58.   LOCATE 1, 1
  59.   FOR Y = 1 TO NumFields        'walk through each field
  60.     PRINT "Field"; Y; TAB(15);  'display each field
  61.     SELECT CASE FieldType(Y)    'see what type of data
  62.       CASE -8                   'double precision
  63.         PRINT CVD(Buffer$(Y))   'so use CVD
  64.       CASE -4                   'single precision
  65.         PRINT CVS(Buffer$(Y))   'as above
  66.       CASE -2                   'integer
  67.         PRINT CVI(Buffer$(Y))
  68.       CASE ELSE                 'string
  69.         PRINT Buffer$(Y)
  70.     END SELECT
  71.   NEXT
  72.   LOCATE 20, 1
  73.   PRINT "Press a key to view the next record ";
  74.   WHILE LEN(INKEY$) = 0: WEND
  75. NEXT
  76. CLOSE #1
  77.  
  78. DATA MYFILE.DAT, 6
  79. DATA 17, 14, -8, -2, 20, -4
  80.